#!/user/bin/env python
# -*-coding:utf-8-*-
'''
@author: Patty
@project: automation_framework
@file: index.py
@time: 2019/7/1 16:01
@IDE: PyCharm 
'''

from datetime import date, timedelta
from util.db_util import execute_query
import logging

today = (date.today()).strftime("%Y-%m-%d")


# 获取当前日期的前一天日期
# today = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")


# 获取24小时最热媒体、最热事件
def get_24hours_hot(type):
    results = execute_query(
        "SELECT oid FROM `nlp_news_tag_hot_daily` WHERE   type = %d ORDER BY dt DESC, std_devide DESC, hot DESC LIMIT 0,10" % (
            type))
    try:
        result = results[0:10]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


# 获取首页标签实体
def get_index_entities(label):
    if label == 'company':
        results = execute_query(
            "SELECT a.code FROM nlp_news_company a JOIN ( SELECT comcode FROM nlp_news_company WHERE tim >'%s' and mkt is not null and mkt !='' and mkt !='N'  GROUP BY comcode ORDER BY COUNT( id ) DESC LIMIT 1 ) b ON a.comcode = b.comcode  ORDER BY a.tim DESC  LIMIT 1" % (
                today))
    elif label == 'concept':
        results = execute_query(
            "SELECT tag_id FROM `nlp_news_tag_hot_daily` WHERE id in (SELECT d.`id` FROM `nlp_news_tag_hot_daily` d JOIN  (SELECT `name`, MAX(tim) tim FROM `nlp_news_tag_hot_daily` WHERE tim >= '%s' AND TYPE = 1 GROUP BY `name`) a ON d.`type` = 1 AND d.`name` = a.name AND d.`tim` = a.tim) ORDER BY std_devide DESC, diff DESC LIMIT 1" % (
                today))
    elif label == 'industry' or label == 'proudect' or label == 'event':
        lable_dict = {"industry": "nlp_news_industry", "proudect": "nlp_news_product", "event": "nlp_news_event"}
        results = execute_query(
            "SELECT code FROM %s WHERE CHAR_LENGTH(`name`) < 6 ORDER BY tim DESC LIMIT 1" % (lable_dict[label]))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None
